<template>
	<view class="user" :class="[isDark?'dark':'', $store.state.AppStyle]" :style="{'background-color':isDark?'#1c1c1c':'#f6f6f6','min-height':isDark?'100vh':'auto'}">
		<view class="header" :style="[{height:CustomBar + 'px'}]">
			<view class="cu-bar" :class="isDark?'dark':'bg-white'" :style="{'height': CustomBar + 'px','padding-top':StatusBar + 'px','background-color':isDark?'#1c1c1c':'#ffffff'}">
				<view class="action" @tap="back">
					<text class="cuIcon-back"></text>
				</view>
				<view class="content text-bold" :style="[{top:StatusBar + 'px'}]">
					{{$t('forum.mypost.mypost')}}
				</view>
				<view class="action" @tap="toWeb">
					{{$t('forum.mypost.contactreview')}}
				</view>
			</view>
		</view>
		<view :style="[{padding:NavBar + 'px 10px 0px 10px'}]"></view>
		
		<view class="data-box">
			<view class="cu-bar search" :class="isDark?'dark':'bg-white'" :style="{'background-color':isDark?'#1c1c1c':'#ffffff'}">
				<view class="search-form round">
					<text class="cuIcon-search"></text>
					<input type="text" placeholder="$t('global.gropekey')" v-model="searchText"  @input="searchTag"/>
					<view class="search-close" v-if="searchText!=''" @tap="searchClose()"><text class="cuIcon-close"></text></view>
				</view>
			</view>
			<view class="search-type grid col-3">
				<view class="search-type-box" @tap="toType(0)" :class="status==0?'active':''">
					<text>{{$t('global.approval')}}</text>
				</view>
				<view class="search-type-box" @tap="toType(2)" :class="status==2?'active':''">
					<text>{{$t('global.locked')}}</text>
				</view>
				<view class="search-type-box" @tap="toType(1)" :class="status==1?'active':''">
					<text>{{$t('global.published')}}</text>
				</view>
			</view>
			<view class="forum-list-main postReview">
				<view class="no-data" v-if="postList.length==0">
					<text class="cuIcon-text"></text>{{$t('common.nodata')}}
				</view>
				<block v-for="(item,index) in postList" :key="index">
					<forumItem :item="item" :myPurview="myPurview"></forumItem>
				</block>
				<view class="load-more" @tap="loadMore" v-if="dataLoad&&postList.length>0">
					<text>{{moreText}}</text>
				</view>
			</view>
		</view>
		
	</view>
</template>

<script>
	import { localStorage } from '../../js_sdk/mp-storage/mp-storage/index.js'
	import darkModeMixin from '@/utils/darkModeMixin.js'
	export default {
		mixins: [darkModeMixin],
		data() {
			return {
				StatusBar: this.StatusBar,
				CustomBar: this.CustomBar,
				NavBar:this.StatusBar +  this.CustomBar,
				AppStyle:this.$store.state.AppStyle,
	
				token:'',
				status:0,
				
				group:"",
				myPurview:0,

				modalName: null,
				
				postList:[],
				topList:[],
				dataLoad:false,
				
				page:1,
				moreText:$t('common.loadmoretxt'),
				
				isLoading:0,
				isLoad:0,
				
				searchText:"",
				
			}
		},
		onPullDownRefresh(){
			var that = this;
			that.page=1;
			that.getPostList(false);
			var timer = setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 1000)
			
		},
		onHide() {
			localStorage.removeItem('getuid')
		},
		onShow(){
			var that = this;
			// #ifdef APP-PLUS
			//plus.navigator.setStatusBarStyle("dark")
			// #endif
		},
		onReachBottom() {
		    //触底后执行的方法，比如无限加载之类的
			var that = this;
			if(that.isLoad==0){
				that.loadMore();
			}
		},
		onLoad(res) {
			var that = this;
			// #ifdef APP-PLUS || MP
			that.NavBar = that.CustomBar;
			// #endif
			if(localStorage.getItem('token')){
				that.token = localStorage.getItem('token');
			}
			that.page=1;
			that.getPostList(false);
		},
		methods: {
			toWeb(){
				var that = this;
				var url = that.$API.SPshenhe();
				// #ifdef APP-PLUS
				plus.runtime.openURL(url) 
				// #endif
				// #ifdef H5
				window.open(url)
				// #endif
			},
			back(){
				uni.navigateBack({
					delta: 1
				});
			},
			toType(type){
				var that = this;
				that.status = type;
				that.page=1;
				that.getPostList(false);
			},
			toSection(){
				uni.navigateTo({
				    url: '/pages/forum/section?type=1'
				});
			},
			move(id){
				this.curId = id;
				uni.navigateTo({
				    url: '/pages/forum/section?type=1'
				});
			},
			searchClose(){
				var that = this;
				that.searchText = "";
				that.page=1;
				that.getPostList(false);
			},
			searchTag(){
				var that = this;
				var searchText = that.searchText;
				that.page=1;
				that.getPostList(false);
			},
			loadMore(){
				var that = this;
				that.moreText = that.$t('global.loading');
				that.isLoad=1;
				that.getPostList(true);
			},
			getPostList(isPage){
				var that = this;
				var page = that.page;
				var token = "";
				var uid = 0;
				if(localStorage.getItem('userinfo')){
					var userInfo = JSON.parse(localStorage.getItem('userinfo'));
					token=userInfo.token;
					uid = userInfo.uid;
				}
				if(isPage){
					page++;
				}
				var data = {
					"status":that.status,
					"authorId":uid
				}
				var order = that.order;
				if(order=='recommend'){
					data.isrecommend = 1;
				}else{
					data.order = order;
				}	
				that.$Net.request({
					url: that.$API.postList(),
					data:{
						"searchParams":JSON.stringify(that.$API.removeObjectEmptyKey(data)),
						"limit":10,
						"page":page,
						"order":'created',
						"searchKey":that.searchText,
						"token":token
					},
					header:{
						'Content-Type':'application/x-www-form-urlencoded'
					},
					method: "get",
					dataType: 'json',
					success: function(res) {
						that.dataLoad =true;
						that.isLoad=0;
						if(res.data.code==1){
							var list = res.data.data;
							if(list.length>0){
								var postList = list;
								if(isPage){
									that.page++;
									that.postList = that.postList.concat(postList);
								}else{
									that.postList = postList;
								}
							}else{
								if(isPage){
									that.moreText = that.$t('common.nomoredata');
								}else{
									that.postList = [];
								}
							}
						}
						var timer = setTimeout(function() {
							that.isLoading=1;
							clearTimeout('timer')
						}, 300)
						that.isLoading = 1;
					},
					fail: function(res) {
						that.dataLoad =true;
						that.isLoad=0;
						that.moreText = that.$t('common.loadmoretxt');
						var timer = setTimeout(function() {
							that.isLoading=1;
							clearTimeout('timer')
						}, 300)
						that.isLoading = 1;
					}
				})
			},

			formatDate(datetime) {
				var datetime = new Date(parseInt(datetime * 1000));
				var year = datetime.getFullYear(),
					month = ("0" + (datetime.getMonth() + 1)).slice(-2),
					date = ("0" + datetime.getDate()).slice(-2),
					hour = ("0" + datetime.getHours()).slice(-2),
					minute = ("0" + datetime.getMinutes()).slice(-2);
				var result = year + "-" + month + "-" + date + " " + hour + ":" + minute;
				return result;
			},
			
			goInfo(id){
				uni.navigateTo({
					url: '/pages/forum/info?id='+id
				});
			},
			toEdit(id){
				uni.navigateTo({
					url: '/pages/forum/info?id='+id
				});
			},
			toDelete(id){
				var that = this;
				var token = "";
				if(localStorage.getItem('userinfo')){
					var userInfo = JSON.parse(localStorage.getItem('userinfo'));
					token=userInfo.token;
				}
				var data = {
					"id":id,
					"token":token
				}
				uni.showModal({
				    title: that.$t('global.suredelpost'),
				    success: function (res) {
				        if (res.confirm) {
				            uni.showLoading({
				            	title: that.$t('common.loadmore')
				            });
				            that.$Net.request({
				            	url: that.$API.postDelete(),
				            	data:data,
				            	header:{
				            		'Content-Type':'application/x-www-form-urlencoded'
				            	},
				            	method: "get",
				            	dataType: 'json',
				            	success: function(res) {
				            		setTimeout(function () {
				            			uni.hideLoading();
				            		}, 1000);
				            		uni.showToast({
				            			title: res.data.msg,
				            			icon: 'none'
				            		})
				            		if(res.data.code==1){
				            			that.page=1;
				            			that.getPostList(false);
				            		}
				            	},
				            	fail: function(res) {
				            		setTimeout(function () {
				            			uni.hideLoading();
				            		}, 1000);
				            		uni.showToast({
				            			title: that.$t('common.networkfail'),
				            			icon: 'none'
				            		})
				            	}
				            })
				        }
				    }
				});
			},
		},
	}
</script>